package goomi.calendar;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import db.mysql.mySqlUtils;

public class calendarService {
	public static void main(String args[]) 
	{
		//test
		try{
//			pictureService color = new pictureService();
//			color.getPicture();
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public ArrayList getCalendar(String date){
		String sql = "select * from calendar ";
		if(date != null && !"all".equals(date)){
			sql += " where date = '" + date + "' ";
		}
		sql += " order by date desc";
		System.out.println(sql);
		return mySqlUtils.Query(sql);
			
	}
	
	
	public void addPicture(String path,String itemId,String itemType){
		Connection conn = null;
		try{
			ArrayList ids = mySqlUtils.Query("select max(CAST(id AS UNSIGNED)) + 1 as id from picture" );
			String id = (String)((Map)ids.get(0)).get("id");
			if(id == null || "".equals(id))id = "1";
			String sql = "insert into picture (id,name,type,path) values ('" +id+ "','" +itemId+ "','" + itemType + "','" + path+ "')";
			mySqlUtils.exec(sql);
			if("goods".equals(itemType)){
				sql = "update goods set picture_id = '" + id + "' where id = '" +itemId + "'"; 
				mySqlUtils.exec(sql);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
	
}
